#!/usr/bin/env bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

if [ -z "$ROOT" ]; then
    echo "error: ROOT should be initialized"
    exit 1
fi

OS=$(go env GOOS)
ARCH=$(go env GOARCH)
OUTPUT=${ROOT}/hack/output
OUTPUT_BIN=${OUTPUT}/bin

KUBECTL_VERSION=1.14.2
KUBECTL_BIN=$OUTPUT_BIN/kubectl

KIND_VERSION=0.7.0
KIND_BIN=$OUTPUT_BIN/kind

test -d "$OUTPUT_BIN" || mkdir -p "$OUTPUT_BIN"

function hack::verify_kubectl() {
    if test -x "$KUBECTL_BIN"; then
        [[ "$($KUBECTL_BIN version --client --short | grep -o -E '[0-9]+\.[0-9]+\.[0-9]+')" == "$KUBECTL_VERSION" ]]
        return
    fi
    return 1
}

function hack::ensure_kubectl() {
    if hack::verify_kubectl; then
        return 0
    fi

    # Use the locally installed kubectl(1.14.2).
    # Because of the development, we are using a specific version of kubectl now.
    # orig_kubectl_bin="$KUBECTL_BIN"
    # if command -v kubectl > /dev/null; then
    #     KUBECTL_BIN="$(command -v kubectl)"
    #     if hack::verify_kubectl; then
    #         ln -sf "$KUBECTL_BIN" "$orig_kubectl_bin"
    #         KUBECTL_BIN="$orig_kubectl_bin"
    #         echo $KUBECTL_BIN
    #         return 0
    #     fi
    # fi

    echo "Installing kubectl v$KUBECTL_VERSION..."
    tmpfile=$(mktemp)
    trap "test -f $tmpfile && rm $tmpfile" RETURN
    curl --retry 10 -L -o $tmpfile https://storage.googleapis.com/kubernetes-release/release/v${KUBECTL_VERSION}/bin/${OS}/${ARCH}/kubectl
    mv $tmpfile $KUBECTL_BIN
    chmod +x $KUBECTL_BIN
}

function hack::verify_kind() {
    if test -x "$KIND_BIN"; then
        [[ "$($KIND_BIN --version 2>&1 | cut -d ' ' -f 3)" == "$KIND_VERSION" ]]
        return
    fi
    return 1
}

function hack::ensure_kind() {
    if hack::verify_kind; then
        return 0
    fi

    # Use the locally installed kind(0.7.0).
    # Because of the development, we are using a specific version of kubectl now.
    # orig_kind_bin="$KIND_BIN"
    # if command -v kind > /dev/null; then
    #     KIND_BIN="$(command -v kind)"
    #     if hack::verify_kind; then
    #         ln -sf "$KIND_BIN" "$orig_kind_bin"
    #         KIND_BIN="$orig_kind_bin"
    #         echo $KIND_BIN
    #         return 0
    #     fi
    # fi

    echo "Installing kind v$KIND_VERSION..."
    tmpfile=$(mktemp)
    trap "test -f $tmpfile && rm $tmpfile" RETURN
    curl --retry 10 -L -o $tmpfile https://github.com/kubernetes-sigs/kind/releases/download/v${KIND_VERSION}/kind-$(uname)-amd64
    mv $tmpfile $KIND_BIN
    chmod +x $KIND_BIN
}

# hack::version_ge "$v1" "$v2" checks whether "v1" is greater or equal to "v2"
function hack::version_ge() {
    [ "$(printf '%s\n' "$1" "$2" | sort -V | head -n1)" = "$2" ]
}
